<?php 
class annonce_chercher extends MX_Controller{
	public $category='';
	public $rechercher='';
	public $region='';
	public $departement='';
	public $codePostal;
	public $type='offre';
	public $nb=0;
	public $nb_particuliers=0;
	public $nb_professionnels=0;
	public $shop_id='';
	public $company='';
	public $option=array();
	public $price=array('min'=>0,'max'=>0);
	//paging
	public $page=0;
	public $limit=15;
	public $paging='';
	
	public function index(){
		
		self::load_session();
		$nom_region='';
		if(isset($_POST['search'])) $this->rechercher=$this->input->post('search');
		if(isset($_POST['search_region'])) $this->region=$this->input->post('search_region');
		if(isset($_POST['search_category'])) $this->category=$this->input->post('search_category');
		if(isset($_POST['search_cp'])){
			$this->codePostal=$this->input->post('search_cp');
				
			if(strlen($_POST['search_cp'])==2 or strlen($_POST['search_cp'])==3)
				$this->departement=$this->input->post('search_cp');
			if(strpos($_POST['search_cp'], ',')===true){
				$extract=explode(',', $this->input->post('search_cp'));
				foreach ($extract as $index=>$idDepartement){
					if(!is_numeric($idDepartement) and ($idDepartement=='2a' or $idDepartement=='2b'))
						unset($extract[$index]);
				}
				$this->region=implode(',', $this->input->post('search_cp'));
			}
		}

		if(isset($_POST['option'])) $this->option=$this->input->post('option');
		
		if(isset($_POST['priceMin'])) $this->price['min']=$this->input->post('priceMin');
		if(isset($_POST['priceMax'])) $this->price['max']=$this->input->post('priceMax');
		
		if(isset($_POST['search_cp']) or isset($_POST['search_category']) or isset($_POST['search_region']) or isset($_POST['search'])) 
			self::set_session();
				
		$TopAnnonces=$annonces=array();
		
		if($this->region){
			$regionObject=$this->model('annonce_models_region')->get_region($this->region);
			$nom_region=$regionObject->nom_region;
		}
		$product=$this->load->Module('product/show');
		$ids_anonnces=self::get_list_annonces();
		
		if(!empty($ids_anonnces))
			foreach ($ids_anonnces as $id_annonce){
				$annonces[$id_annonce]=$product->get_info($id_annonce);
			}
		
		
		
		$message_tous =$this->nb;
		$message_particuliers=$this->nb_particuliers;
		$message_professionnels =$this->nb_professionnels;
		 
		$title='Rechercher '.$this->rechercher;
		$description='Resultats '.$this->rechercher;  
		

		$keywords=$this->rechercher;
		$message_tous.=' annonces';
		$message_particuliers.=' annonces' ;
		$message_professionnels.=' annonces'; 
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'nb'=>$this->nb,
				'nb_particulier'=>$this->nb_particuliers,
				'nb_professionnells'=>$this->nb_professionnels,
				'paging'=>$this->paging,
				'message_tous'=>$message_tous,
				'message_particuliers'=>$message_particuliers ,
				'message_professionnels'=>$message_professionnels,
				'annonces'=>$annonces,
				'TopAnnonces'=>$TopAnnonces
		);

		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		$type=$this->session->userdata('type');
		if($type=='') $type='offre';
		$data['nav_class'][$type]='active';
		
		$this->view(
				array('header',
						'category',
						'footer'),
				$data); // Display the page
		
	}
	public function page($page=''){
		if($page=='')$page=0;
		$this->page=$page;
		self::index();
	}
	
	public function filtre(){
		$this->load->library('session');
		$this->session->set_userdata('filtre_type',$this->input->post('type'));
		$this->load->helper('url');
		$current_url=$this->input->post('current_url');
		$extract=explode('/page',$current_url);
		if(isset($extract[0]))$current_url=$extract[0];
		redirect($current_url, 'refresh');
	}
	public function get_list_annonces(){
		$departement= $this->region ? explode(',',$this->region):'';
		if($this->departement)$departement=array($this->departement);
		$paging=array('number_affiche'=>$this->limit,'page_current'=>$this->page);
		$word=$this->rechercher;
		//en tête, priorité
		$sort=array('top'=>'DESC','priority'=>'DESC','date_validate'=>'DESC');
		$options=$this->option;
		//par defaut annonce offre et active
		$filter=array('prd_type'=>$this->type,'prd_active'=>1,'prd_price'=>$this->price);
		
		if(is_array($departement))$filter['departement']=$departement;
		if(strlen($this->codePostal)==5) $filter['prd_codepostal']=$this->codePostal;
		if($this->category!='') $filter['category']=$this->category; 
		if($this->shop_id!='')$filter['shop_id']=$this->shop_id;
		if(is_numeric($this->company)) $filter['company']= $this->company;
		
		$ids_anonnces=array();
		$rows=$this->model('product_models_admin')->get_list_product($filter,$options,$sort,$word,$paging);

		$this->nb=$this->session->userdata('total_annonce');
		$this->nb_particuliers=$this->session->userdata('total_particuliers');
		$this->nb_professionnels=$this->session->userdata('total_professionels');
		
		foreach ($rows as $row) $ids_anonnces[]= $row->prd_id; 
		//$ids_anonnces=$this->model()->get_annonces($departement,$this->codePostal,$this->category,$this->rechercher,$this->type,$this->option,$this->shop_id,$this->company,$this->page,$this->limit);
		$sort=array();
		/*
		if(isset($filter['company'])) unset($filter['company']); 
		$row=$this->product_models_admin->get_list_product($filter,$options,$sort,$word,'',true);

		$this->nb=isset($row[0])?$row[0]->total:0;
		
		$filter['company']= 0;
		$row=$this->product_models_admin->get_list_product($filter,$options,$sort,$word,'',true);
		$this->nb_particuliers=isset($row[0])?$row[0]->total:0;
		
		$filter['company']= 1;
		$row=$this->product_models_admin->get_list_product($filter,$options,$sort,$word,'',true);
		$this->nb_professionnels=isset($row[0])?$row[0]->total:0;
		*/
		//paging
		$Total=$this->nb;
		if (is_numeric($this->company)) {
			if($this->company) $Total=$this->nb_professionnels;
			else $Total=$this->nb_particuliers;
		}
		
		if($Total>$this->limit){
			$this->load->helper('url');
			$paging='';
			/* $current_url=current_url();*/
					
			$current_url=uri_string();
			$extract=explode('/page',$current_url);
			if(isset($extract[0]))$current_url=$extract[0];
			
			$this->load->library('pagination');
			$config['base_url'] = '/'.$current_url.'/page/';
			$config['cur_page'] = $this->page;
				
			$config['total_rows'] = $Total;
			$config['per_page'] = $this->limit;
			$config['next_tag_open'] =$config['last_tag_open'] =$config['first_tag_open'] =$config['prev_tag_open'] =$config['num_tag_open'] = '<li>';
			$config['next_tag_close'] =$config['last_tag_close']  =$config['first_tag_close'] =$config['prev_tag_close'] =$config['num_tag_close'] = '</li>';
			$config['cur_tag_open'] =' <li class="disabled"><a href="#">';
			$config['cur_tag_close']='</a></li>';
			$this->pagination->initialize($config);
			$this->paging=$this->pagination->create_links();
		}
		//end paging
		
		return $ids_anonnces;
	}
	public function load_session(){
		$this->load->library('session');

		$this->category=$this->session->userdata('category_id');
		$this->codePostal=$this->session->userdata('search_cp');
		$this->rechercher=$this->session->userdata('search');
		$this->region=$this->session->userdata('search_region');
		$this->option=$this->session->userdata('option');
		$type=$this->session->userdata('type');
		if($type=='')
			$this->session->set_userdata('type',$this->type);
		else
			$this->type=$type;
		
		if($this->region=='')$this->region=$this->session->userdata('region');

		$filtre_type=$this->session->userdata('filtre_type');
		
		switch ($filtre_type) {
			case 'Professionnels':
				 $this->company=1;
				break;
			case 'Particuliers':
				$this->company=0;
				break;
			default: $this->company='';
		}
			
		
	}
	public function set_session($newdata =array()){

		$this->load->library('session');
		if(empty($newdata))
			$newdata = array(
					'search' => $this->rechercher,
					'search_cp'     => $this->codePostal,
					'search_region'     => $this->region,
					'region'     => $this->region,
					'category_id'  => $this->category,
					'option'=> $this->option,
					'price'=> $this->price,
					'type'  => $this->type
			);
			
		$this->session->set_userdata($newdata);
		
	}
	
}